package cn.ulyer.core.modules.system.rest;


import cn.hutool.core.lang.Assert;
import cn.ulyer.common.annotation.Log;
import cn.ulyer.common.constants.ResultCode;
import cn.ulyer.common.jpa.entity.*;
import cn.ulyer.common.model.PageableVo;
import cn.ulyer.common.utils.PageResult;
import cn.ulyer.common.utils.Result;
import cn.ulyer.core.modules.system.service.RolePermissionService;
import cn.ulyer.core.modules.system.service.RoleService;
import cn.ulyer.core.modules.system.vo.PermissionVo;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * by generator - rest
 */
@RestController
@RequestMapping("/tRoleRest")
public class TRoleRest {

    private final static String MODULE = "role";

    @Autowired
    private RoleService tRoleService;

    @Autowired
    private RolePermissionService rolePermissionService;


    /**
     * 分页
     */
    @GetMapping("/page")
    public Result<PageResult<Role>> page(PageableVo pageableVo, Role tRole){
        PageResult result = tRoleService.page(pageableVo.render(),tRole);
        return Result.ok().data(result);
    }

    @GetMapping("/allRole")
    public Result<List<Role>> list(){
        return Result.ok().data(tRoleService.findAll());
    }

    @GetMapping("/get")
    public Result<Role> detail(@RequestParam Integer id){
        return Result.ok().data(tRoleService.getOne(id));
    }

    @PostMapping("/create")
    @Log(module = MODULE,action = Log.ActionName.CREATE)
    public Result<Integer> create(@RequestBody Role tRole){
        tRoleService.save(tRole);
        return Result.ok(ResultCode.CREATE).data(tRole.getId());
    }


    @PutMapping("/update")
    @Log(module = MODULE,action = Log.ActionName.UPDATE)
    public Result update(@RequestBody Role tRole){
        tRoleService.updateById(tRole);
        return Result.ok(ResultCode.UPDATE);
    }

    /**
     * removeById
     *
     */
    @DeleteMapping("/remove")
    @Log(module = MODULE,action = Log.ActionName.DELETE)
    public Result remove(@RequestParam Integer id){
        tRoleService.deleteById(id);
        return Result.ok(ResultCode.DELETE);
    }

    @GetMapping("/listRolePermission/{roleId}")
    public Result<List<PermissionVo>> listByRoleId(@PathVariable Integer roleId){
        QRolePermission rolePermission = QRolePermission.rolePermission;
        return Result.ok().data( rolePermissionService.findAll(rolePermission.roleId.eq(roleId)));
    }


    /**
     * key  : permissions :id[] roleId: id
     * @param jsonObject
     * @return
     */
    @PutMapping("/updateRolePermission")
    public Result updateRolePermission(@RequestBody JSONObject jsonObject){
        Integer roleId = jsonObject.getInteger("roleId");
        Assert.notNull(roleId);
        List<Integer> permission = jsonObject.getJSONArray("permissions").toJavaList(Integer.class);
        rolePermissionService.updateRolePermissions(roleId,permission);
        return Result.ok();
    }


}
